package com.yesorno.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.utils.HttpUtils;

public class ImageDetailActivity extends CustomTitleActivity {
	RegBtnsClickListeners rbc = new RegBtnsClickListeners();

	private Button goBack;
	private Button save;
	private Button img_detail_small;
	private Button img_detail_big;

	private ImageView img_detail_image;

	private LinearLayout img_detail_image_layout;

	private Bitmap bitmap;

	private double scale = 1.0;
	private double perScale = 0.05;

	/**
	 * 注册页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class RegBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == goBack) {
				Intent intent = new Intent();
				ImageDetailActivity.this.setResult(RESULT_OK, intent);
				ImageDetailActivity.this.finish();
			}
			if (v == save) {

			}
			if (v == img_detail_big) {
				scale = scale + perScale;
				resetImageScale(bitmap, scale);
			}
			if (v == img_detail_small) {
				scale = scale - perScale;
				resetImageScale(bitmap, scale);
			}
		}

	}

	public void resetImageScale(Bitmap bmp, double scale) {
		if (scale < 0) {
			return;
		}
		int bmpWidth = bitmap.getWidth();
		int bmpHeight = bitmap.getHeight();
		/* 计算出这次要缩小的比例 */
		int scaleWidth = (int) (bmpWidth * scale);
		int scaleHeight = (int) (bmpHeight * scale);

		img_detail_image_layout.removeView(img_detail_image);

		img_detail_image = new ImageView(ImageDetailActivity.this);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				scaleWidth, scaleHeight);
		img_detail_image_layout.addView(img_detail_image);
		img_detail_image.setLayoutParams(lp);
		/* 产生reSize后的Bitmap对象 */
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizeBmp = Bitmap.createScaledBitmap(bitmap, bmpWidth,
				bmpHeight, false);
		// Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
		// matrix, true);//会报错
		img_detail_image.setImageBitmap(resizeBmp);
	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();
		goBack = CustomTitleActivity.createTitleLeftButton(
				ImageDetailActivity.this, R.string.reg_title_btn_goback,
				Constants.TITLE_BTN_MARGIN1);
		goBack.setOnClickListener(rbc);
		titleLayout.addView((View) goBack.getParent());
		TextView tv = CustomTitleActivity.createTitleDesc(
				ImageDetailActivity.this, R.string.img_detail_title, 0);
		titleLayout.addView((View) tv.getParent());

		save = CustomTitleActivity.createTitleRightButton(
				ImageDetailActivity.this, R.string.img_detail_save,
				Constants.TITLE_BTN_MARGIN2);
		save.setOnClickListener(rbc);
		LinearLayout ll = (LinearLayout) save.getParent();
		ll.setGravity(Gravity.RIGHT);
		titleLayout.addView((View) save.getParent());

	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.img_detail);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		img_detail_image = (ImageView) findViewById(R.id.img_detail_image);
		String url = getIntent().getStringExtra("imgPath");
		bitmap = HttpUtils.returnBitMap(ImageDetailActivity.this,url);
		img_detail_image.setImageBitmap(bitmap);
		img_detail_image.setOnClickListener(rbc);

		img_detail_image_layout = (LinearLayout) findViewById(R.id.img_detail_image_layout);

		img_detail_small = (Button) findViewById(R.id.img_detail_small);
		img_detail_big = (Button) findViewById(R.id.img_detail_big);
		img_detail_small.setOnClickListener(rbc);
		img_detail_big.setOnClickListener(rbc);

		resetImageScale(bitmap, 1);
	}
}
